package com.msb.servicemap.remote;

import com.msb.internalcommon.constant.AmapConfigConstants;
import com.msb.internalcommon.dto.ResponseResult;
import com.msb.internalcommon.reponese.TerminalResponse;
import com.msb.internalcommon.reponese.TrackResponse;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @Auther: jiudianliu
 * @Date: 2023/7/9 - 07 - 09 - 10:43
 * @Description: com.msb.servicemap.remote
 * @version: 1.0
 */
@Service
public class TrackClient {

    @Value("${map.key}")
    private String key ;

    @Value("${map.sid}")
    private String sid ;

    @Autowired
    private RestTemplate restTemplate ;

    public ResponseResult<TerminalResponse> add (String tid) {
        //拼装请求的url
        //网址示例：?keywords=北京&subdistrict=2&key=<用户的key>
        StringBuilder url = new StringBuilder();
        url.append(AmapConfigConstants.TRACK_ADD);
        url.append("?");
        url.append("key="+key);
        url.append("&");
        url.append("sid="+sid);
        url.append("&");
        url.append("tid="+tid);
        ResponseEntity<String> forEntity = restTemplate.postForEntity(url.toString(), null , String.class);
        String body = forEntity.getBody();
        JSONObject jsonObject = JSONObject.fromObject(body);
        JSONObject data = jsonObject.getJSONObject("data");
        //轨迹ID
        String trid = data.getString("trid");
        //轨迹名称
        String trname = " " ;
        if (data.has("trname")) {
            trname = data.getString("trname") ;
        }
        TrackResponse trackResponse = new TrackResponse();
        trackResponse.setTrid(trid);
        trackResponse.setTrname(trname);
        return ResponseResult.success(trackResponse) ;
    }
}
